package cn.iocoder.yudao.module.service.controller.admin.stepform;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;

import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.service.controller.admin.stepform.vo.*;
import cn.iocoder.yudao.module.service.dal.dataobject.stepform.StepFormDO;
import cn.iocoder.yudao.module.service.service.stepform.StepFormService;

@Tag(name = "管理后台 - 表单")
@RestController
@RequestMapping("/service/step-form")
@Validated
public class StepFormController {

    @Resource
    private StepFormService stepFormService;

    @PostMapping("/create")
    @Operation(summary = "创建表单")
    public CommonResult<Long> createStepForm(@Valid @RequestBody StepFormSaveReqVO createReqVO) {
        return success(stepFormService.createStepForm(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新表单")
    public CommonResult<Boolean> updateStepForm(@Valid @RequestBody StepFormSaveReqVO updateReqVO) {
        stepFormService.updateStepForm(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除表单")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteStepForm(@RequestParam("id") Long id) {
        stepFormService.deleteStepForm(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得表单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<StepFormRespVO> getStepForm(@RequestParam("id") Long id) {
        StepFormDO stepForm = stepFormService.getStepForm(id);
        return success(BeanUtils.toBean(stepForm, StepFormRespVO.class));
    }

    @GetMapping("/get-by-step")
    @Operation(summary = "获得根据步骤表单")
    @Parameter(name = "stepId", description = "编号", required = true, example = "1024")
    public CommonResult<StepFormRespVO> getFormByStep(@RequestParam("stepId") Long stepId) {
        StepFormDO stepForm = stepFormService.getStepFormByStepId(stepId);
        return success(BeanUtils.toBean(stepForm, StepFormRespVO.class));
    }


    @GetMapping("/page")
    @Operation(summary = "获得表单分页")
    public CommonResult<PageResult<StepFormRespVO>> getStepFormPage(@Valid StepFormPageReqVO pageReqVO) {
        PageResult<StepFormDO> pageResult = stepFormService.getStepFormPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, StepFormRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出表单 Excel")
    @OperateLog(type = EXPORT)
    public void exportStepFormExcel(@Valid StepFormPageReqVO pageReqVO,
                                    HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<StepFormDO> list = stepFormService.getStepFormPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "表单.xls", "数据", StepFormRespVO.class,
                BeanUtils.toBean(list, StepFormRespVO.class));
    }

}